<?php

namespace app\home\controller;

use app\admin\model\Cate;
use app\admin\model\NewsModel;
use app\home\model\Comment;
use app\home\model\News;
use think\Controller;
use think\Db;

class Index extends Controller
{
     //前台首页
      public function index(){
          //1.查询分类数据
          $cate=Cate::all()->toArray();
          //2.图片新闻
          $img_news=News::where(['data_flag'=>1])->field('id,title,pic_url')->limit(5)->order('view_nums','desc')->select()->toArray();
          //3.最新新闻
          $new_news=News::where(['data_flag'=>1])->field('id,title,add_time')->limit(10)->order('add_time','desc')->select()->toArray();
          //4.最热新闻
          $hot_news=News::where(['data_flag'=>1])->field('id,title,add_time')->limit(10)->order('view_nums','desc')->select()->toArray();
          //为了做样式，默认设置cate_id=0
          $cate_id=0;
          return $this->fetch('index',compact('cate','img_news','new_news','hot_news','cate_id'));
      }

      //分类新闻
       public function lists(){
            //1.接收分类id
            $cate_id=input('cate_id');
            //根据分类id查询图片新闻和最新新闻
           //2.图片新闻
           $img_news=News::where(['data_flag'=>1,'cate_id'=>$cate_id])->field('id,title,pic_url')->limit(5)->order('view_nums','desc')->select()->toArray();
           //3.最新新闻
           $new_news=News::where(['data_flag'=>1,'cate_id'=>$cate_id])->field('id,title,add_time')->limit(10)->order('add_time','desc')->select()->toArray();
           //4.新闻分类
           $cate=Cate::all()->toArray();
           //获取该id对于的新闻分类名称
           $cate_name=Cate::find($cate_id)->cate_name;
           return $this->fetch('index_list',compact('cate','img_news','new_news','cate_name','cate_id'));
       }

      //新闻详情页面
      public function detail(){
          //1.查询分类数据
          $cate=Cate::all()->toArray();
          //2.接收新闻id,查询出新闻数据
          $id=input('get.id');
          $new=NewsModel::find($id)->toArray();
          $cate_id=$new['cate_id'];
          //3.查询出该新闻下的所有的评论
          $comments=Db::table('news_comment')
              ->field('c.id,c.content,c.add_time,u.username')
              ->alias('c')
              ->join(['news_news'=>'n'],'c.news_id=n.id')
              ->join(['news_user'=>'u'],'c.user_id=u.id')
              ->where('c.news_id',$id)
              ->order('c.add_time','desc')
              ->select();

          return $this->fetch('news_content',compact('cate','new','cate_id','comments'));
      }
      //评论新闻
     public function comment(){
          //做登录校验
          if(!session('?user_id')){
              return $this->error('请先登录再发表评论','user/login');
          }
          //接收新闻id
          $news_id=input('post.news_id');
          //接收新闻内容
          $content=input('post.content','');
          if($content==''){
              return $this->error('评论内容不能为空');
          }
          //组装
          $data['news_id']=$news_id;
          $data['content']=$content;
          $data['add_time']=date('Y-m-d H:i:s');
          $data['user_id']=session('user_id');
          $comment_model=new Comment();
          $res=$comment_model->save($data);
          if($res){
              return $this->success('发表成功');
          }else{
              return $this->error('发表失败');
          }
     }

}
